package com.dogstudio.mygame.view;

import android.app.Activity;
import android.app.Dialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.dogstudio.mygame.R;
import com.dogstudio.mygame.config.Config;
import com.dogstudio.mygame.utils.AppUtils;

/**
 * Created by Administrator on 2016/8/20 0020.
 */

public class ResultDialog extends Dialog {
    private Activity mContext;
    private OnOkListener mListener;
    private TextView mTvTitle;
    private TextView mTvContent;
    private Button mBtnCancel;
    private Button mBtnRestart;

    public ResultDialog(Activity context, OnOkListener l) {
        super(context, R.style.DlgStyle);
        mContext = context;
        mListener = l;
        initView();
        setSize();
    }

    private void initView() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.dlg_result, null);
        mTvTitle = (TextView) view.findViewById(R.id.tv_title);
        mTvContent = (TextView) view.findViewById(R.id.tv_content);
        mBtnCancel = (Button) view.findViewById(R.id.btn_cancel);
        mBtnRestart = (Button) view.findViewById(R.id.btn_restart);
        mBtnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mListener != null) {
                    mListener.onCancel();
                }
                dismiss();
            }
        });
        mBtnRestart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mListener != null) {
                    mListener.onOk();
                }
                dismiss();
            }
        });
        if (!Config.IS_DEBUG) {
            setCanceledOnTouchOutside(false);
        }
        setContentView(view);
    }

    public void setMsg(String title, String content) {
        mTvTitle.setText(title);
        mTvContent.setText(content);
    }


    @Override
    public void show() {
        if (mContext != null && !mContext.isFinishing()) {
            super.show();
        }
    }

    @Override
    public void dismiss() {
        if (mContext != null && !mContext.isFinishing()) {
            super.dismiss();
        }
    }

    private void setSize() {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = (int) (AppUtils.getScreenWidth(mContext) * 0.7);
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes(lp);
    }


    public interface OnOkListener {
        void onOk();

        void onCancel();
    }

}
